package cn.tedu.sentineldeom.controller;

import cn.tedu.sentineldeom.service.HelloService;
import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class HelloController {

    @Autowired
    private HelloService helloService;

    @GetMapping("/hello")
//    添加该注解后，当前资源就会被sentinel进行管理
    //限流默认错误提示，不够友好，我们可以自定义限流方法
    //blockHandler = "blockFunc",指定的是限流方法名
    @SentinelResource(value = "sayHi",
            blockHandlerClass = Block.class, blockHandler ="blockFunc",
            fallbackClass = Fallback.class, fallback = "fallbackFunc"
           )
    public String sayHi(String name){
        String result = helloService.sayhi(name);
        try {
            Thread.sleep(500);
//            模拟处理失败，方便测试降级效果
//            Math.random() 0——————1
            if (Math.random()<0.8){
//                1/0--->使系统报错 出现不合法的运算；
//                如何卡在这里---》降级---自定义降级类
               int num = 1/0;
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return result;
    }



}
